#include "userDao.h"
void addUser(User *user){
    sqlite3 *db = NULL;
    //打开db
    int res = sqlite3_open("/home/hsh/ronghuanet_ads/test.db",&db);
    if (res != 0)
    {
        perror("open db failed");
        return;
    }
    char *sql = (char*)calloc(1,256);
    sprintf(sql,"insert into t_user(id,name) values(%d,'%s')",user->id,user->name);
    char *errMsg;
    sqlite3_exec(db,sql,NULL,NULL,&errMsg);
    if (errMsg != NULL)
    {
        printf("%s\n",errMsg);
    }
    sqlite3_close(db);
}
void getUserAll(){
    sqlite3 *db = NULL;
    //打开db
    int res = sqlite3_open("/home/hsh/ronghuanet_ads/test.db",&db);
    if (res != 0)
    {
        perror("open db failed");
        return;
    }
    char *sql = "select * from t_user";
    char *errMsg;
    int row;
    int column;
    //接收结果集
    char **result;  
    sqlite3_get_table(db,sql,&result,&row,&column,&errMsg);
    printf("row total count:%d\n",row);
    printf("column total count:%d\n",column);
    int index = column;
    printf("查到%d条数据\n",row);
    for (int i = 0; i < row; i++)
    {
        printf("第%d条数据\n",i + 1);
        for (int j = 0; j < column; j++)
        {
            printf("字段名:%s > 字段值:%s\n",result[j],result[index]);    
            ++index;
        }
    }
}